﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Shop
{
    [Access("Consultant")]
    public partial class Consultant : PageBase
    {
        private byte ModelId;
        private readonly ConsultantManage _myBLL = new ConsultantManage();

        private int _ID
        {
            get
            {
                if (ViewState["CT_ID"] != null)
                {
                    return Convert.ToInt32(ViewState["CT_ID"]);
                }
                return 0;
            }
            set { ViewState["CT_ID"] = value; }
        }

        private void Bind(int PageNo)
        {
            var pp = new PagingParam();
            pp.PageNo = PageNo;
            pp.PageSize = this.PageNavigation1.PageSize;
            this.Repeater1.DataSource = this._myBLL.BindList(pp);
            this.Repeater1.DataBind();

            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Bind(1);
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var iDs = GetSelectedRow.GetIDs(this.Repeater1);
            var res = this._myBLL.Delete(iDs);
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this);
                return;
            }
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            Bind(1);
        }

        protected void btnReplyOK_Click(object sender, EventArgs e)
        {
            var model = new WinnerCMS.Model.Consultant
            {
                Id = _ID,
                ReplyContent = this.txtReplyContent.Text,
                ReplyTime = DateTime.Now,
                Replier = UserManage.Current.UserName
            };
            string err = null;
            this._myBLL.Reply(model, ref err);
            if (!string.IsNullOrEmpty(err))
            {
                MsgBox.Message(err, this);
            }
            else
            {
                this.Panel1.Visible = true;
                this.Panel2.Visible = false;
                Bind(1);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ModelId = byte.Parse(Request["ModelId"]);
            if (!Page.IsPostBack)
            {
                var str = string.Format("{0} → <a href='Consultant_Manage.aspx?ModelId={1}'>咨询管理</a>",
                    ModelManage.GetModelName(this.ModelId), this.ModelId);
                this.lblPath.Text = str;
                Bind(1);
                this.Panel1.Visible = true;
                this.Panel2.Visible = false;
            }
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            Bind(e.NewPageIndex);
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var commandName = e.CommandName;
            if (commandName != null)
            {
                if (!(commandName == "Reply"))
                {
                    if (commandName == "DeleteGB")
                    {
                        this._myBLL.Delete(e.CommandArgument.ToString());
                        this.Panel1.Visible = true;
                        this.Panel2.Visible = false;
                        Bind(1);
                    }
                }
                else
                {
                    _ID = Convert.ToInt32(e.CommandArgument);
                    var consultant = this._myBLL.ReplyLoad(_ID);
                    if (consultant != null)
                    {
                        this.L_Content.Text = consultant.Content;
                        this.txtReplyContent.Text = consultant.ReplyContent;
                        this.Panel1.Visible = false;
                        this.Panel2.Visible = true;
                    }
                }
            }
        }
    }
}